package com.neusoft.neumooc.controller.front;

import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.neusoft.core.common.BaseController;

/**
 * @ClassName: DownLoadController
 * @Description: TODO 下载
 * @author Zhenglishen
 * @date 2016年8月25日
 */
@Controller
@RequestMapping(value = "/frontDownload")
public class DownLoadController extends BaseController {

	
	/**
	 * @Title: downloadclick
	 * @Description: TODO 文件下载
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/downLoadClick")
	@ResponseBody
	public void downLoadClick122(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		try {
			String file = request.getParameter("file");
			String fullFileName = request.getServletContext().getRealPath(
					"/resources/upload/uploadfile/" + file);// 获得完整的地址
			// 解决中文乱码问题
			String userAgent = request.getHeader("USER-AGENT");
			if (StringUtils.contains(userAgent, "MSIE")) {// IE浏览器
				file = URLEncoder.encode(file, "UTF8");
			} else if (StringUtils.contains(userAgent, "Mozilla")) {// google,火狐浏览器
				file = new String(file.getBytes(), "iso-8859-1");
			} else {
				file = URLEncoder.encode(file, "UTF8");// 其他浏览器
			}
			response.setContentType(request.getServletContext().getMimeType(
					file));
			InputStream in = new FileInputStream(fullFileName);
			OutputStream out = response.getOutputStream();
			// 写文件
			int b;
			while ((b = in.read()) != -1) {
				out.write(b);
			}
			in.close();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
